package org.example.struct.array;

/**
 * @author liyishan
 * @date 2023/8/24 11:06
 * @apiNote
 */

public class M03FindNumberIn2DArray {
    public static void main(String[] args) {
        int[][]  matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        boolean flag= test1(matrix,5);
        System.out.println(flag);
    }

    public static boolean test(int[][] matrix, int target) {
        for (int[] row : matrix) {
            for (int element : row) {
                if (element == target) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean test1(int[][] matrix, int target) {
        for (int[] row : matrix) {
            int index = search(row, target);
            if (index >= 0) {
                return true;
            }
        }
        return false;
    }

    public static int search(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        while (low <= high) {
            int mid = (high - low) / 2 + low;
            int num = nums[mid];
            if (num == target) {
                return mid;
            } else if (num > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

}
